<?php
namespace org\crawlgenie\application\document;
use org\crawlgenie\application\filter\Filter;
use \CGException;

/**
 * Description of DocumentJSON
 *
 * @author Jinson
 */
class DocumentJSON extends Document {
    private $_finalRawContent = null;
    

    public function __construct(IRenderable $renderer = null) {
        parent::__construct($renderer);
    }


    /**
     * Standard getInstance method for singletons to get a single cached instance.
     */
    public static function getInstance() {
        if(self::$_instance == null) {
            $class = __CLASS__;
            self::$_instance = new $class;
        }
        return self::$_instance;
    }


    public function setHeader() {
        header('Content-type: application/json; charset=utf-8'); //change to text/plain if there're problems
    }

    
    /*
     * Check for any arrays within the variables to render and json them first
     */
    public function parsePage() {
        $rawContent = $this->getToRenderArray();

        //assign by reference so we can modify the array directly
        foreach($rawContent as &$content) {
            if(is_array($content)) {
                $content = Filter::jsonEncode($content, true);
            }
        }
        $this->_finalRawContent = $rawContent;
    }
    

    public function sendDocument() {
        print Filter::jsonEncode($this->_finalRawContent);
    }


}
?>
